<?php
$content = '';
if (!empty($options)){
	$fn_zen_href_link_parameters1 = (isset($_GET['cPath'])?'cPath='.$_GET['cPath'].'&':'').(isset($_GET['options_values_id'])?'options_values_id='.$_GET['options_values_id'].'_':'options_values_id='); 
	$fn_zen_href_link_parameters2 = (isset($_GET['cPath'])?'cPath='.$_GET['cPath'].'&':'');
	$_GET_options_values_id = array_unique(array_map('zen_string_to_int',explode('_',$_GET['options_values_id'])));
	$content_group_choosed = '';
	$the_options_values_choosed = array();
	$sql = 'select p.products_id from '.TABLE_PRODUCTS_ATTRIBUTES.' pa1 '.
	       'JOIN '.TABLE_PRODUCTS.' p ON p.products_id=pa1.products_id ';
	$join_cPath = $where_cPath = $join_options = $where_options = $group_options = '';
	if (isset($_GET['cPath'])){
		$join_cPath = 'JOIN '.TABLE_PRODUCTS_TO_CATEGORIES.' p2c on p2c.products_id=p.products_id '; 
		$where_cPath = 'AND p2c.categories_id IN('.implode(',',explode('_', $_GET['cPath'])).')';
	}
	if (isset($_GET['options_values_id'])){
		$join_options = 'JOIN '.TABLE_PRODUCTS_ATTRIBUTES.' pa2 ON pa2.products_id=p.products_id ';
		$where_options ='AND pa2.options_values_id IN('.implode(',',explode('_', $_GET['options_values_id'])).')';
		$group_options = 'group by p.products_id having count(*)>='.count(explode('_', $_GET['options_values_id']));
	}
	$sql = 'SELECT count(*) as count from ('.
			$sql.$join_cPath.$join_options.'WHERE pa1.options_values_id=%s AND p.products_status=1 '.$where_cPath.$where_options.$group_options.
			') as t';
	
	foreach ($options as $products_options_id=>$products_options_values){
		//判断是当前选项是否有被选择
		$intersect = array_intersect(array_keys($products_options_values),$_GET_options_values_id);
		if (empty($intersect)){
			$content .= '<div class="options_group"><div class="options_group_header">▼Shop BY '.current(current($products_options_values)).'</div><div class="options_group_body">';
			foreach ($products_options_values as $products_options_values_id=>$option_info){
				$sql_count = sprintf($sql,$products_options_values_id);
				$count = $db->Execute($sql_count);
				if ($count->fields['count']>0)
					$content .= '<div><a href="'.zen_href_link(FILENAME_DEFAULT,$fn_zen_href_link_parameters1.$products_options_values_id).'">'.$option_info['products_options_values_name'].'('.$count->fields['count'].')</a></div>';
			}
			$content .= '</div></div>';
		}else {
			$content_group_choosed .= '<div class="options_group_choosed"><div class="options_group_header">▼Shop BY '.current(current($products_options_values)).'</div><div class="options_group_body">';
			foreach ($intersect as $products_options_id){
				$str = implode('_',array_diff ($_GET_options_values_id,array($products_options_id)));
				$str = empty($str)?'':'options_values_id='.$str;
				$the_options_values_choosed[] = array('products_options_values_id'=>$products_options_id,'products_options_name'=>$products_options_values[$products_options_id]['products_options_values_name']);
				$content_group_choosed .= '<div><a href="'.zen_href_link(FILENAME_DEFAULT,$fn_zen_href_link_parameters2.$str).'">'.$products_options_values[$products_options_id]['products_options_values_name'].'</a></div>';
			}
			$content_group_choosed .= '</div></div>';
		}		
	}
	$content = '<div class="leftBoxContainer" id="shop_by" style="width:'.BOX_WIDTH_LEFT.'"><div class="shop_by_header">REFINE BY'.(empty($_GET['options_values_id'])?'':'<a style="float:right;font-weight:normal" href="'.zen_href_link(FILENAME_DEFAULT,(isset($_GET['cPath'])&&$_GET['cPath']!='')?'cPath='.$_GET['cPath']:'',true).'">clear all</a>').'</div>'.$content_group_choosed.$content.'</div>';
}
echo $content;